//
//  WJTabBarViewController.m
//  WJBSApp
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJTabBarViewController.h"
#import "WJ_Essence_VC.h"
#import "WJ_New_VC.h"
#import "WJ_Publish_VC.h"
#import "WJ_Follow_VC.h"
#import "WJFollowAndMeNAVC.h"

#import "WJMEVC.h"

@interface WJTabBarViewController ()

@property (nonatomic,retain) WJBaseButton *previousButton;

@end

@implementation WJTabBarViewController

-(void)dealloc
{
    [_wjTabbar release];
    [_previousButton release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.wjTabbar = [[WJBaseImageView alloc]initWithFrame:self.tabBar.frame];
    [self.view addSubview:self.wjTabbar];
    [_wjTabbar release];
    
    // 开启用户交互
    self.wjTabbar.userInteractionEnabled = YES;
    
    [self createView];
    
}
- (void)createView
{
    // 精华
    WJ_Essence_VC *essence = [[WJ_Essence_VC alloc]init];
    WJBaseNavigationController *essenceNAVC = [[WJBaseNavigationController alloc]initWithRootViewController:essence];
    [self createButtonNormalName:@"tabBar_essence_icon@3x副本.png" SelectedName:@"tabBar_essence_click_icon@3x副本.png" index:0];
    [self addChildViewController:essenceNAVC];
    [essenceNAVC release];
    [essence release];
    
    // 新帖
    WJ_New_VC *new = [[WJ_New_VC alloc]init];
    WJBaseNavigationController *newNAVC = [[WJBaseNavigationController alloc]initWithRootViewController:new];
    [self createButtonNormalName:@"tabBar_new_icon@3x副本.png" SelectedName:@"tabBar_new_click_icon@3x副本.png" index:1];
    [self addChildViewController:newNAVC];
    [new release];
    [newNAVC release];
    
    // 中间的
    WJBaseButton *publishButton = [[WJBaseButton alloc]initWithFrame:CGRectMake(WIDTH / 5 * 2, 0, WIDTH / 5, 49)];
    [self.wjTabbar addSubview:publishButton];
    [publishButton release];
    [publishButton setImage:[UIImage imageNamed:@"tabBar_publish_icon@3x副本"] forState:UIControlStateNormal];
    [publishButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon@3x副本"] forState:UIControlStateSelected];
    [publishButton addTarget:self action:@selector(publishButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 关注
    WJ_Follow_VC *follow = [[WJ_Follow_VC alloc]init];
    WJFollowAndMeNAVC *followNAVC = [[WJFollowAndMeNAVC alloc]initWithRootViewController:follow];
    [self createButtonNormalName:@"tabBar_friendTrends_icon@3x副本" SelectedName:@"tabBar_friendTrends_click_icon@3x副本.png" index:3];
    [self addChildViewController:followNAVC];
    [followNAVC release];
    [follow release];
    
    // 我
    WJMEVC *me = [[WJMEVC alloc]init];
    WJFollowAndMeNAVC *meNAVC = [[WJFollowAndMeNAVC alloc]initWithRootViewController:me];
    [self createButtonNormalName:@"tabBar_me_icon@3x副本" SelectedName:@"tabBar_me_click_icon@3x副本.png" index:4];
    [self addChildViewController:meNAVC];
    [meNAVC release];
    [me release];
    
    // 默认选中第一个button
    WJBaseButton *btn = self.wjTabbar.subviews[0];
    [self changeViewController:btn];
    
    
}

- (void)createButtonNormalName:(NSString *)normalName SelectedName:(NSString *)selectedName index:(NSInteger)index{
    
    WJBaseButton *button = [WJBaseButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(WIDTH / 5 * index, 0, WIDTH / 5, self.tabBar.frame.size.height);
    [self.wjTabbar addSubview:button];
    [button setImage:[UIImage imageNamed:normalName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectedName] forState:UIControlStateDisabled];
    if (index == 0 || index == 1 ) {
        button.tag = index;
    }else{
        button.tag = index - 1;
    }
    
    [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchDown];
}


#pragma mark - 按钮动作方法 , 切换页面
- (void)changeViewController:(WJBaseButton *)button
{
    // 设置选中的控制器
    
    self.selectedIndex = button.tag;
    // 先令button 失效
    button.enabled = NO;
    
    //  如果上一次点击的button 不是这次点击的button 令其恢复
    if (self.previousButton != button) {
        self.previousButton.enabled = YES;
    }
    self.previousButton = button;
}

// 中间按钮的点击方法
- (void)publishButton:(WJBaseButton *)button
{
    WJ_Publish_VC *vc = [[WJ_Publish_VC alloc]init];
    [self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];
}

#pragma mark - 实现了隐藏TabBar 但是使用方法应该在需要隐藏的那一页使用 

-(void)setHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed{
    self.wjTabbar.hidden = hidesBottomBarWhenPushed;
}



//- (void)isHiddenTabBar:(BOOL)isHidden withViewController:(UIViewController *)viewController{
//    // 先删除自己定义的
//    [self.wjTabbar removeFromSuperview];
//    if (isHidden) {// 如果是隐藏的状态
//        self.tabBar.hidden = NO;
//        [self.wjTabbar setFrame:self.tabBar.frame];
//        [self.tabBar addSubview:self.wjTabbar];
//        if (!viewController) {
//            viewController.hidesBottomBarWhenPushed = YES; // push到下个页面后隐藏tabBar
//        }
//    }
//    else
//    {
//        [self.tabBar setHidden:YES];
//        [self.wjTabbar setFrame:self.tabBar.frame];
//        [self.view addSubview:self.wjTabbar];   // 将自定义的tabBar 添加到View上
//    }
//
//}

//获得自定义Tabbar

//    WJTabBarViewController *tabbar = (WJTabBarViewController *)self.tabBarController;
//    [tabbar isHiddenTabBar:NO withViewController:self];

















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
